<?php
namespace app\agent\controller;

use app\lib\exception\LoginException;
use app\lib\exception\SuccessMessage;
use think\Request;
use app\agent\model\Key as KeyModel;
use app\agent\model\Agent;
use think\paginator;
use app\admin\model\Log;


class Key extends Base {
    public function index() {

        $this->assign('list', (new KeyModel)->getIndexContent());
        return view();
    }
    
    //查看代理商的激活码详情
    public function keyDetail() {
        $id = intval(Request::instance()->param('id'));
        $detail = KeyModel::where(array('agent_id' => $id))->where('is_active', 'neq', '-1')->paginate(20);

        $this->assign('agent', Agent::where(array('id' => $id))->find());
        $this->assign('list', $detail);
        return view();
    }

    //删除激活码
    public function del() {
        $id = Request::instance()->post('id');
        $res = KeyModel::where(array('id' => $id))->update(array('is_active' => -1));

        if($res) {
            $log = array(
                '被删除激活码ID',
                $id
            );
            $this->saveLog('删除激活码', $log);

            throw new SuccessMessage([
                'nsg' => '删除成功'
            ]);
        }else {
            throw new LoginException([
                'msg' => '删除失败'
            ]);
        }
    }

    //分配代理商激活码
    public function allotCode() {
        $id = intval(Request::instance()->param('id'));

        if(Request::instance()->post()) {
            $total = Request::instance()->post('quantity') ? Request::instance()->post('quantity') : 0;//分配的数量
            $postData = Request::instance()->post();

            if($total>($postData['num1']-$postData['num2']) || $total <= 0) {
                throw new LoginException([
                    'msg' => '没有足够的激活码，或者输入不合法'
                ]);
            }
            $res = (new keyModel)->distributeCode($postData['agent_id'], $postData['agent_name'], $total);

            if($res) {
                $log = array(
                    '代理商名字',
                    $postData['agent_name'],
                    '代理商ID',
                    $postData['agent_id'],
                    '分配激活码个数',
                    $res['num'],
                    '分配激活码的ID',
                    $res['data']
                );
                $this->saveLog('分配代理商激活码', $log);

                throw new SuccessMessage([
                    'msg' => '成功分配'.$postData['agent_name'].'代理商 '.$res['num'].' 个激活码'
                ]);
            }else {
                throw new LoginException([
                    'msg' => $this->error()
                ]);
            }
        }

        $num = KeyModel::where('is_active', 'eq', '1')->where(array('agent_id' => session('agentId')))->count();
        $this->assign('total_key', KeyModel::where('is_active', 'neq', '-1')->where(array('agent_id' => session('agentId')))->count());
        $this->assign('is_active', $num);
        $this->assign('agent_info', Agent::where(array('id' => $id))->field('id,name')->find());
        return view();
    }

    //激活码操作日志
    public function log() {
        $this->assign('list', Log::where(array('operation_id' => session('agentId')))->order('create_time', 'DESC')->select());
        return view();
    }

    //日志详情
    public function LogDetail() {
        $id = Request::instance()->param('id');
        $list = Log::where('id', 'eq', $id)->find();
        $data = (array)json_decode($list['data']);

        $this->assign('data', $data);
        $this->assign('list', $list);
        return view();
    }

    //存日志
    public function saveLog($msg, $list = array()) {
        $log = array(
            'action' => $msg,
            'operation_id' => session('agentId'),
            'operation_name' => session('agentName').'(代理商)',
            'data' => json_encode($list)
        );
        Log::create($log);
    }
}